import pandas as pd
import seaborn as sns
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"
For this excercise, we have written the following code to load the stock dataset built into plotly express.
stocks = px.data.stocks() #I changed the index
stocks.head()
Select a stock and create a suitable plot for it. Make sure the plot is readable with relevant information, such as date, values.
plt.figure(figsize=(15,10))
plt.plot(stocks["AMZN"])
plt.title("Amazon stock")
plt.xticks([0,14,28,42,56,70,84,98])
plt.xlabel("date");
plt.ylabel("Stock value");
You've already plot data from one stock. It is possible to plot multiples of them to support comparison.
To highlight different lines, customise line styles, markers, colors and include a legend to the plot.
ST=["GOOG","AAPL","AMZN","FB","NFLX","MSFT"]
plt.figure(figsize=(15,10))
for i in range(len(ST)):
plt.plot(stocks[ST[i]])
plt.title("Stocks")
plt.xticks([0,14,28,42,56,70,84,98])
plt.xlabel("Date");
plt.legend(["Google","Apple","Amazon","FaceBook","Netflix","Microsoft"])
plt.ylabel("Stock value");
First, load the tips dataset
tips = sns.load_dataset('tips')
tips.head()
Let's explore this dataset. Pose a question and create a plot that support drawing answers for your question.
Some possible questions:
#Do female tip more than males during lunch or Dinner
plt.figure(figsize=(15,10))
plt.subplot(121)
tips['Tip_per_bill'] = tips["tip"]/tips["total_bill"]*100
Dinner = tips[tips["time"].str.contains('Dinner')]
Lunch = tips[tips["time"].str.contains('Lunch')]
Dinner.head()
sns.boxplot(data=(Dinner), x="sex", y="Tip_per_bill")
plt.ylim(0, 75)
plt.title("Diner")
plt.ylabel("Percentage tip as part of the total bill")
plt.subplot(122)
sns.boxplot(data=(Lunch), x="sex", y="Tip_per_bill")
plt.title("Lunch")
plt.ylim(0, 75)
plt.ylabel("Percentage tip as part of the total bill");
For dinner the average female tipping is higger than males.
For lunch the males do tip slightly higger
Redo the above exercises (challenges 2 & 3) with plotly express. Create diagrams which you can interact with.
Hints:
ST=["GOOG","AAPL","AMZN","FB","NFLX","MSFT"]
Google=pd.DataFrame(stocks[["date","GOOG"]])
GG = Google.rename(columns={"GOOG":"stock value"})
GG["Company"] = "Google"
Apple=pd.DataFrame(stocks[["date","AAPL"]])
AA = Apple.rename(columns={"AAPL":"stock value"})
AA["Company"] = "Apple"
Amazon=pd.DataFrame(stocks[["date","AMZN"]])
ZZ = Amazon.rename(columns={"AMZN":"stock value"})
ZZ["Company"] = "Amazon"
Facebook=pd.DataFrame(stocks[["date","FB"]])
FF = Facebook.rename(columns={"FB":"stock value"})
FF["Company"] = "Facebook"
Netflix=pd.DataFrame(stocks[["date","NFLX"]])
NN = Netflix.rename(columns={"NFLX":"stock value"})
NN["Company"] = "Netflix"
Microsoft=pd.DataFrame(stocks[["date","MSFT"]])
MM = Microsoft.rename(columns={"MSFT":"stock value"})
MM["Company"] = "Mircosoft"
frames=[GG, AA, ZZ,FF,NN,MM]
result = pd.concat(frames)
fig = px.line(result, x="date", y="stock value", color="Company")
fig.show();
fig =px.box(tips, x="time", y="Tip_per_bill", color="sex", labels={"Tip_per_bill":"Percentage tip as part of the total bill"}, title="Do female tip more than males during lunch or Dinner")
fig.show()
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
#load data
df = px.data.gapminder()
df.head()
seven = df[df["year"]==2007]
sev =seven.groupby(seven["continent"]).sum()
ves= sev.reset_index()
fig = px.bar(ves,y="continent", x="pop",color="continent", text="pop")
fig.update_yaxes(categoryorder="total ascending")
fig.show()